package com.chent.jackson;

import com.alibaba.fastjson.JSONObject;
import com.chent.pojo.Student;
import com.chent.utils.Examination;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 * @author chent
 */
public class Base {
    public Student newStudent() {
        Student student = new Student();
        student.setAge(34);
        student.setName("张三");
        student.setAddress("成都");
        List<String> hobbys = new ArrayList<>();
        hobbys.add("足球");
        hobbys.add("篮球");
        hobbys.add("乒乓球");
        student.setHobby(hobbys);
        return student;
    }

    @Test
    public void Test01() throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();
        Examination.start();
        objectMapper.writeValue(new File("json/student.json"), newStudent());
        Examination.end();
    }

    @Test
    public void Test02() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        Examination.start();
        Student student = objectMapper.readValue(new File("json/student.json"), Student.class);
        System.out.println(student);
        Examination.end();
    }

    @Test
    public void Test03() {
        Examination.start();
        JSONObject jsonObject = new JSONObject(5, true);
        jsonObject.fluentPut("name", "笑话").fluentPut("age", 34).fluentPut("idea", "good").fluentPut("hello", "world");
        System.out.println(jsonObject.toJSONString());
        Examination.end();
    }
}
